#pragma once

#include <string>
#include <iostream>
#include <functional>

namespace ThreadMoudule
{
    enum class TSTATUS
    {
        NEW,
        START,
        STOP
    };
    using func_t = std::function<void()>;
    static int number = 1;
    class thread
    {
    private:
        static void *Routine(void *args)
        {
            thread*t=static_cast<thread*>(args);
            t->_status = TSTATUS::START;
            t->_func();
            return nullptr;
        }

    public:
        thread(func_t func) : _func(func)
        {
            _name = "Thread-" + std::to_string(number++);
        }
        bool Start()
        {
            if (_status != TSTATUS::START)
            {
                int n = ::pthread_create(&_tid, nullptr, Routine, this);
                if (n != 0) return false;
                return true;
            }
            return true;
        }
        bool Stop()
        {
            int n=::pthread_cancel(_tid);
            if(n!=0) return false;
            _status=TSTATUS::STOP;
            return true;
        }
        bool Join()
        {
            int n=::pthread_join(_tid,nullptr);
            if(n==0) return true;
            return false;
        }
        void EnableDetach()
        {
             
        }
        ~thread()
        {
        }

    private:
        std::string _name;
        pthread_t _tid;
        pid_t _pid;
        bool _joinable; // 是否分离，默认不是
        func_t _func;
        TSTATUS _status;
    };

}